<?php

namespace App\Http\Controllers\Api;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\MShopGood;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ShopGoodController extends ApiController
{

    /**
     * 商品列表
     *
     */
    public function goods_list(Request $request){
        $page = $request->page??1;
        $size = $request->size??10;
        $start_count = ($page-1)*$size;
        $type = $request->type??1;
        $where['is_sale'] = "on";
        if($type){
            $where['operator'] = $type;
        }
        $where['deleted_at'] = null;

        $this->data['total'] = MShopGood::where($where)->count();
        $goods_list = MShopGood::where($where)->orderBy('sort', 'desc')->offset($start_count)->limit($size)->get();
        $this->data['goods_list'] = $goods_list;

        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

    /**
     * 可返佣商品
     *
     */
    public function goods_return(){
        $page = $request->page??1;
        $size = $request->size??10;
        $start_count = ($page-1)*$size;
        $where['is_sale'] = "on"; // 在售
        $where['is_earnings'] = 1; // 返佣
        $where['deleted_at'] = null;

        $this->data['total'] = MShopGood::where($where)->count();

        $goods_list = MShopGood::where($where)->orderBy('sort', 'desc')->offset($start_count)->limit($size)->get();
        $this->data['goods_list'] = $goods_list;

        return $this->ReturnMessage(200,getMessage(),$this->data);
    }

    /**
     * 商品详情
     *
     */
    public function goods_detail(Request $request){
        $goods_id = $request->goods_id;
        if ( !$goods_id ) {
            return $this->ReturnMessage(-1, '参数错误');
        }

        $goods_detail = MShopGood::where(['id'=> $goods_id])->first();
        $return_data = $goods_detail ? $goods_detail->toArray(): [];

        return $this->ReturnMessage(200,'查询成功', $return_data);
    }

    /**
     * 获取创建订单信息
     *
     */
    public function get_order_info(Request $request){
        $goods_id = $request->goods_id;

        if ( !$goods_id ) {
            return $this->ReturnMessage(-1, '参数错误');
        }

        $goods_detail = MShopGood::where(['id'=> $goods_id, 'is_sale'=> 'on'])->first();
        $return_data = $goods_detail ? $goods_detail->toArray(): [];
        Log::info('获取创建订单信息_获取商品信息', $return_data);
        $phone = '';
        $place = '';

        if(!empty($return_data)){
            // 判断商品信息中是否有归属地
            if ( $return_data['is_owner'] == 1 && $return_data['owner_place'] != 'X' ) {
                $place_data = config("move.ownership");
                $place = $place_data[$return_data['owner_place']];
                $phone = $this->rand_select_phone($return_data['owner_place']);

                $log = [
                    '$phone' => $phone,
                ];
                Log::info('获取创建订单信息', $log);
                
                /*if ( !$phone ) {
                    return $this->ReturnMessage(-1, '参数有误');
                }*/
            }
        }

        $return_data['phone'] = $phone??'';
        $return_data['place'] = $place??'';
        $return_data['info'] = '根据国家实名制要求，请准确提供身份证信息，信息内容采用加密传输方式，保证您的信息安全。';

        return $this->ReturnMessage(200,'查询成功', $return_data);
    }

    /**
     * 随机选取一个手机号
     * @param string $part  归属地
     *
     * @return array
     */
    public function rand_select_phone($part, $type=1){
        Log::info('rand_select_phone方法的$part', [$part]);

        if ( !$part ) return false;

        $data = [];
        if ($type == 1) {
            // 移动号池
            $code = 'UIP_CARD_QRYPOOL_RT';
            $data = $this->getPhoneNumber($code, $part);
            Log::info('rand_select_phone方法的$data', [$data]);
        } elseif($type == 2) {
            // 联通号池
            $data['code'] == 201;
        } else {
            // 电信号池
            $data['code'] == 201;
        }
        $returnArray = [];
        if ( $data['code'] == 200 ) {
            $numberArray = $data['data']['cardpool'];

            if(!empty($numberArray)){
                // 循环遍历出所有的手机号码
                foreach ($numberArray as $key=> $value) {
                    $returnArray[$key] = [
                        'owner_place' => $value['CARDREGION'],
                        'number' => $value['CARDSVCNUM'],
                    ];
                }
            }
        }

        if ($returnArray) {
            $key = array_rand($returnArray, 1);
            $phone_number = $returnArray[$key]['number'];
        } else {
            return false;
        }


        return $phone_number;
    }
}
